package com.nyist.Controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nyist.dao.CustomUserRepository;
import com.nyist.entity.User;
import com.nyist.repository.UserRepository;
import com.nyist.util.TransDate;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private CustomUserRepository customUserRepository;
	
	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("/test")
	public List<User> findUserBetweenAge(@RequestParam(required = true) Integer startAge,
										 @RequestParam(required = true) Integer endAge) {
		List<User> users = customUserRepository.findByAgeWithRangeFilter(startAge, endAge);
	    return users;
	}
	
	@RequestMapping("/findAll")
	public List<User> findAllUser() {
		List<User> users = customUserRepository.findAll();
		
		return users;
	}
	
	@RequestMapping("/addUser")
	public String addUser(User user, @RequestParam(required = true) String date) {
		Date birthday = TransDate.strToDate(date);
		user.setBirthday(birthday);
		User users = userRepository.save(user);
		if (null != users) {
			return "success";
		}
		return "failure";
	}
}
